package com.estronger.shopmanage.ui.measure.pg;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.tasking.PgMsDetailListAdapter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B101PgMeasureList;
import com.estronger.shopmanage.bean.B16PlanMeasure;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.view.AppDialog;
import com.estronger.shopmanage.view.CustomListView;
import com.lidroid.xutils.http.RequestParams;

public class MsPgDetailActivity extends BaseActivity implements
        View.OnClickListener {

    private MsPgDetailActivity mActivity;
    private View rootView;
    private ScrollView sv_root;
    private B101PgMeasureList.Datas.PgMeasureListData comData;
    private String measureid;
    private TextView tv_title, tvfName, tvfAddress, tvfSales, tvfOrderTimeT, tvfOrderTime;
    private ImageView ivfCallToCustomer, ivfSendToCustomer, ivfCallToSales, ivfSendToSales;
    private String cstName, cstphoneNumber, salesName, salesphoneNumber;
    private CustomListView goodsListView;
    private FloatingActionButton fab;
    private Dialog dialog;

    @Override
    protected void init() {
        comData = (B101PgMeasureList.Datas.PgMeasureListData) getIntent().getSerializableExtra("measureItem");
        if (comData != null) {
            measureid = comData.measureid;
        }
    }

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mActivity = MsPgDetailActivity.this;
        rootView = View.inflate(mActivity, R.layout.activity_pg_msdetail, null);
        setContentView(rootView);
        tv_title = (TextView) findViewById(R.id.ab_title);
        tv_title.setText("测量任务详情");
        sv_root = (ScrollView) findViewById(R.id.detail_pgscroller);

        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "getplanmeasure");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("measureid", measureid);
        connect(params, NetManager.PG_MS_DETAIL);
        fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, MsPGTripActivity.class));
            }

            ;
        });
        initTitle(sv_root);
        goodsListView.setFocusable(false);
    }

    @Override
    protected void initAction() {
        super.initAction();
        setTextContent();
        goodsListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
    }

    private void initTitle(View root) {
        tvfName = (TextView) root.findViewById(R.id.tvfItemTitle);
        tvfAddress = (TextView) root.findViewById(R.id.tvfItemLocation);
        tvfSales = (TextView) root.findViewById(R.id.tvfSales);
        tvfOrderTimeT = (TextView) root.findViewById(R.id.tvfOrderTimeT);

        tvfOrderTime = (TextView) root.findViewById(R.id.tvfOrderTime);

        ivfCallToCustomer = (ImageView) root.findViewById(R.id.ivfMCall);
        ivfCallToSales = (ImageView) root.findViewById(R.id.ivfMCallSales);
        ivfSendToCustomer = (ImageView) root.findViewById(R.id.ivfMSendms);
        ivfSendToSales = (ImageView) root.findViewById(R.id.ivfMSendToSales);

        goodsListView = (CustomListView) root.findViewById(R.id.cLvfGoods);

        ivfCallToCustomer.setOnClickListener(this);
        ivfCallToSales.setOnClickListener(this);
        ivfSendToCustomer.setOnClickListener(this);
        ivfSendToSales.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivfMSendms:
                if (isRightNumber(cstphoneNumber)) {
                    Uri uri = Uri.parse("smsto:" + cstphoneNumber);
                    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
                break;
            case R.id.ivfMSendToSales:
                if (isRightNumber(salesphoneNumber)) {
                    Uri uri = Uri.parse("smsto:" + salesphoneNumber);
                    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                    startActivity(intent);
                }
                break;
            case R.id.ivfMCall:
                if (isRightNumber(cstphoneNumber)) {
                    dialog = AppDialog.createDialog(this, cstName, cstphoneNumber, this, measureid);
                    dialog.show();
                }
                break;
            case R.id.ivfMCallSales:
                if (isRightNumber(salesphoneNumber)) {
                    dialog = AppDialog.createDialog(this, salesName, salesphoneNumber,
                            this, measureid);
                    dialog.show();
                }
                break;
            case R.id.ok:
                if (PermissionUtils.isCallPhoneGranted(mActivity)) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                            + salesphoneNumber));
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    startActivity(intent);
                } else {
                    PermissionUtils.ToSetPermissions(mActivity, R.string.PermissionsCallPhone);
                }
                dialog.dismiss();
                break;
        }
    }

    /**
     * 当前activity请求网络成功的回调
     *
     * @params code 各个接口返回的唯一识别码，用来判断是哪个接口成功返回
     * @params result 网络请求成功返回的数据
     */
    @Override
    protected void onNetSuccess(int code, String result) {
        // Log.i("result", "DETAIL = " + result);
        super.onNetSuccess(code, result);
        if (NetManager.PG_MS_DETAIL == code) {
            showCompleshView(result);
        }
    }
    //
    public void showCompleshView(String result) {
        B16PlanMeasure detail = gson.fromJson(result, B16PlanMeasure.class);
        B16PlanMeasure.PlanMeasureData pData = detail.data;
        if (pData == null) {
            return;
        }
        salesName = pData.navname;
        tvfSales.setText(salesName);
        salesphoneNumber = pData.navmobile;
        PgMsDetailListAdapter pdAdapter = new PgMsDetailListAdapter(mActivity, pData.measureitem);
        goodsListView.setAdapter(pdAdapter);
//        if (TextUtils.isEmpty(pData.buildname)) {
//            cstName = pData.cstname;
//        } else {
//            cstName = pData.buildname + "-" + pData.cstname;
//        }
//        tvfName.setText(cstName);
//        tvfAddress.setText(pData.address);
//        cstphoneNumber = pData.cstmobile;
    }

    private void setTextContent() {

        if (comData == null) {
            return;
        }
        if (TextUtils.isEmpty(comData.cstbuildname)) {
            cstName = comData.cstname;
        } else {
            cstName = comData.cstbuildname + "-" + comData.cstname;
        }
        tvfName.setText(cstName);
        tvfAddress.setText(comData.address);
        tvfOrderTimeT.setText("下单时间:");
        tvfOrderTime.setText(comData.ordrdate);
        cstphoneNumber = comData.cstmobile;
    }
}

